<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "comment".
 *
 * @property integer $id
 * @property integer $post_id
 * @property string $comment
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 */
class Comment extends \yii\db\ActiveRecord
{
    
    const IS_VALID = 1;
    const NO_VVALID = 0;
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'comment';
    }
    
    /**
     * 自动填充 created_at 和 updated_at 字段
     * {@inheritDoc}
     * @see \yii\base\Component::behaviors()
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['post_id', 'comment', 'created_at', 'updated_at'], 'required'],
            [['id', 'post_id', 'status'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['comment'], 'string', 'max' => 255],
        ];
    }
    
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'post_id' => 'Post ID',
            'comment' => 'Comment',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
    
    public function getUserName()
    {
        $result = Comment::find()->with('user')->asArray()->one();
        
        return $result?:[];
    }
}
